package com.jeeplus.modules.guide.utils;

import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class HttpQueueUtils {
    private static final Logger logger = LoggerFactory.getLogger(HttpQueueUtils.class);
    public static String intoQueue(String url,String idcard,String name,String typeNumber,String areaId){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "{\"cmd\":9999,\"data\":{\"dao\":\"outInterfaceDAO\",\"action\":\"sendNumber\",\"sendAddress\":\"取号\",\"models\":{\"stype\":\"1\",\"sysId\":\""+areaId+"\",\"counter\":\"0\",\"card\":\"{\\\"card\\\":\\\""+idcard+"\\\",\\\"name\\\":\\\""+name+"\\\",\\\"gender\\\":\\\"2\\\",\\\"national\\\":\\\"01\\\",\\\"birthday\\\":\\\"\\\",\\\"address\\\":\\\"\\\",\\\"IssuingAuthority\\\":\\\"\\\",\\\"effective\\\":\\\"20170203-20370203\\\",\\\"qita\\\":\\\"\\\"}\",\"cardName\":\"\",\"ywSerial\":\""+typeNumber+"\",\"ywName\":\"\"}}}");
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "text/plain")
                .build();
        try{
            Response response = client.newCall(request).execute();
            return response.body().string().toString();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("get street is error ："+e.getMessage());
        }
        return null;
    }

    public static String intoQueueOne(String url,String idcard,String name,String typeNumber,String areaId){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "{\"cmd\":9999,\"data\":{\"dao\":\"outInterfaceDAO\",\"action\":\"sendNumber\",\"sendAddress\":\"取号\",\"models\":{\"stype\":\"1\",\"sysId\":\""+areaId+"\",\"counter\":\"0\",\"card\":\"{\\\"card\\\":\\\""+idcard+"\\\",\\\"name\\\":\\\""+name+"\\\",\\\"gender\\\":\\\"2\\\",\\\"national\\\":\\\"01\\\",\\\"birthday\\\":\\\"\\\",\\\"address\\\":\\\"\\\",\\\"IssuingAuthority\\\":\\\"\\\",\\\"effective\\\":\\\"20170203-20370203\\\",\\\"qita\\\":\\\"\\\"}\",\"cardName\":\"\",\"ywSerial\":\""+typeNumber+"\",\"ywName\":\"\"}}}");
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "text/plain")
                .build();
        try{
            Response response = client.newCall(request).execute();
            return response.body().string().toString();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("get street is error ："+e.getMessage());
        }
        return null;
    }

    public static String outQueue(String url,String areaId){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "{\"cmd\":9999,\"data\":{\"dao\":\"outInterfaceDAO\",\"action\":\"userDiscard\",\"sendAddress\":\"弃号\",\"models\":{\"sysId\":\""+areaId+"\",\"counter\":\"0\"}}}");
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "text/plain")
                .build();
        try{
            Response response = client.newCall(request).execute();
            return response.body().string().toString();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("get street is error ："+e.getMessage());
        }
        return null;
    }
    public static String passQueue(String url,String idcard,String areaId,String number,String sType){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "{\"cmd\":9999,\"data\":{\"dao\":\"outInterfaceDAO\",\"action\":\"setQueueHist\",\"sendAddress\":\"设定号码状态\",\"models\":{\"sysId\":\""+areaId+"\",\"counter\":\"0\",\"card\":\""+idcard+" \",\"number\":\""+number+"\",\"stype\":\""+sType+"\"}}}");
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "text/plain")
                .build();
        try{
            Response response = client.newCall(request).execute();
            logger.error(response.body().string());
            return response.body().string().toString();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("get street is error ："+e.getMessage());
        }
        return null;
    }
    public static String setVoice(String url,String context){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "<?xml version=\"1.0\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"><SOAP-ENV:Body xmlns:NS1=\"urn:etyy_dataIntf-Ietyy_data\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><NS1:getCmd><s xsi:type=\"xsd:string\">{\"cmd\":1001,\"data\":{\"host\":\"10.31.172.44\",\"params\":{\"isQZ\":\"1\",\"context\":\""+context+"\",\"num\":1,\"APKVIDEO\":{\"isPercent\":1,\"ip\":\"127.0.0.1\",\"s\":0.4,\"v\":0.9,\"toip\":\"127.0.0.1\"}}}}</s></NS1:getCmd></SOAP-ENV:Body></SOAP-ENV:Envelope>");
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "text/plain")
                .build();
        try{
            Response response = client.newCall(request).execute();
            return response.body().string().toString();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("get street is error ："+e.getMessage());
        }
        return null;
    }
    public static String QueueSync(Map<String,Object> map,String url){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url(url +
                        "?type=" +map.get("type")+
                        "&date=" +map.get("date")+
                        "&number=" +map.get("number")+
                        "&winnumber=" +map.get("winnumber")+
                        "&time=" +map.get("time")+
                        "&stime=" +map.get("stime")+
                        "&sign="+map.get("sign"))
                .method("GET", null)
                .addHeader("Cookie", "PHPSESSID=dla0b0dh9r96b0as1ki2i9lkp4")
                .build();

        try{
            Response response = client.newCall(request).execute();
            return response.body().string().toString();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("get street is error ："+e.getMessage());
        }
        return null;
    }
}
